package city.spring.modules.medicalstation.drugdepositDrugExt.service.impl;

import city.spring.modules.medicalstation.drugdepositDrugExt.entity.DrugdepositDrugExtEntity;
import city.spring.modules.medicalstation.drugdepositDrugExt.repository.DrugdepositDrugExtRepository;
import city.spring.modules.medicalstation.drugdepositDrugExt.service.DrugdepositDrugExtService;
import city.spring.modules.medicalstation.drugdepositDrugExt.vo.DrugdepositDrugExtVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.List;

/**
 * Service：药品缴存与药品关联表
 *
 * @author 谢金婷
 * @date 2020-10-13 16:56:39
 */
@CacheConfig(cacheNames = {DrugdepositDrugExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DrugdepositDrugExtServiceImpl extends ServiceImpl<DrugdepositDrugExtRepository, DrugdepositDrugExtEntity> implements DrugdepositDrugExtService {
    private final DrugdepositDrugExtRepository drugdepositDrugExtRepository;

    public DrugdepositDrugExtServiceImpl(DrugdepositDrugExtRepository drugdepositDrugExtRepository) {
        this.drugdepositDrugExtRepository = drugdepositDrugExtRepository;
    }

    @Override
    public void saveDrugdepositDrugExt(DrugdepositDrugExtEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDrugdepositDrugExt(DrugdepositDrugExtEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        entity.setRemainAmount(entity.getTotalAmount());
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteDrugdepositDrugExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteDrugdepositDrugExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public void refundDeposit(String id, DrugdepositDrugExtEntity entity) {
        if (entity.getStatus().equals(2)) {
            JOptionPane.showMessageDialog(null, "该药品已经是退缴状态！", "错误", JOptionPane.ERROR_MESSAGE);
        }
        if (entity.getStatus().equals(1)) {
            entity.setStatus(2);
            JOptionPane.showMessageDialog(null, "退缴成功！", "信息", JOptionPane.ERROR_MESSAGE);
        }
        drugdepositDrugExtRepository.refundDeposit(id, entity);
    }

    @Override
    public List<DrugdepositDrugExtVO> getDrugdepositDrugByOldmanId(String oldmanId) {
        return drugdepositDrugExtRepository.getDrugdepositDrugByOldmanId(oldmanId);
    }

    @Override
    public void deleteDrugsettingsDrugExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public List<DrugdepositDrugExtVO> getByOldmanId(String oldmanId) {
        return drugdepositDrugExtRepository.getByOldmanId(oldmanId);
    }

    @Override
    public IPage<DrugdepositDrugExtVO> listPage(Page<? extends DrugdepositDrugExtEntity> page, Wrapper<? extends DrugdepositDrugExtEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}